from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QHBoxLayout, QLineEdit, QLabel, QFileDialog
from projectManager import *


class CreateProjectDialog(QDialog):
    def __init__(self, projectHandle: ProjectManager):
        super().__init__()
        self.setWindowTitle('New Project')
        self.projectHandle = projectHandle
        btn1 = QPushButton('create', self)
        btn1.clicked.connect(self.create)
        btn2 = QPushButton('close', self)
        btn2.clicked.connect(self.close)
        btn3 = QPushButton('browse', self)
        btn3.clicked.connect(self.browse)

        label = QLabel("Project Name:")
        self.lineEdit = QLineEdit()
        hlayout1 = QHBoxLayout()
        hlayout1.addWidget(label)
        hlayout1.addWidget(self.lineEdit)

        label_path = QLabel("Project Path:")
        self.lineEdit_path = QLineEdit()
        hlayout2 = QHBoxLayout()
        hlayout2.addWidget(label_path)
        hlayout2.addWidget(self.lineEdit_path)

        hlayout3 = QHBoxLayout()
        hlayout3.addWidget(btn3)
        hlayout3.addWidget(btn1)
        hlayout3.addWidget(btn2)

        layout = QVBoxLayout()
        layout.addLayout(hlayout1)
        layout.addLayout(hlayout2)
        layout.addLayout(hlayout3)
        self.setLayout(layout)

        self.show()

    def create(self):
        self.projectHandle.setProjectName(self.lineEdit.text())
        self.close()

    def browse(self):
        path = QFileDialog.getExistingDirectory(self, "Open file", ".")
        self.lineEdit_path.setText(path)
        self.projectHandle.setProjectPath(path)


if __name__ == "__main__":
    import sys

    pm = ProjectManager()
    app = QtWidgets.QApplication(sys.argv)
    ui = CreateProjectDialog(pm)
    ui.show()
    sys.exit(app.exec_())
